package com.wbcs.jbsf.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.Config;

/**
 * JBSF层封装
 * @author hawkfly
 */
public class JBSFDaoTemplate
{
    private boolean isCloseConn = true;
    private static Log log=LogFactory.getLog(JBSFDaoTemplate.class);
    
    public JBSFDaoTemplate(boolean isCloseConn){
        this.isCloseConn = isCloseConn;
    }
    public <T> T select(String sql, IDataModal<T> dataModal, Object...params)throws SQLException{
        T t = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        List<Object> lstparams = acceptAryParams(params);
        Connection conn = DataSourceManager.getInstance().getConnection();
        try
        {
            ps = conn.prepareStatement(sql);
            for(int i=1;i<=lstparams.size();i++)
            {
                ps.setObject(i,lstparams.get(i-1));
                if(Config.show_sql){
                    log.info("sql param("+i+": "+lstparams.get(i-1)+")");
                }
            }
            if(Config.show_sql)
            {
                log.info("Execute sql:"+sql);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData(); int ccount = rsmd.getColumnCount();
            t = dataModal.loadDatas(rs,ccount, rsmd, t);
        }finally{
        	if(rs!=null)rs.close();
            if(ps!=null)ps.close();
            if(conn!=null)conn.close();
        }
        return t;
    }
    
    public <T> T select(String sql, Connection conn, IDataModal<T> dataModal, Object...params)throws SQLException{
        T t = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        List<Object> lstparams = acceptAryParams(params);
        if(conn == null){
            conn = DataSourceManager.getInstance().getConnection();
            isCloseConn = true;
        }
        try
        {
            ps = conn.prepareStatement(sql);
            for(int i=1;i<=lstparams.size();i++)
            {
                ps.setObject(i,lstparams.get(i-1));
                if(Config.show_sql){
                    log.info("sql param("+i+": "+lstparams.get(i-1)+")");
                }
            }
            if(Config.show_sql)
            {
                log.info("Execute sql:"+sql);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData(); int ccount = rsmd.getColumnCount();
            t = dataModal.loadDatas(rs,ccount, rsmd, t);
        }finally{
            if(rs!=null)rs.close();
            if(ps!=null)ps.close();
            if(isCloseConn&&conn!=null)conn.close();
        }
        return t;
    }
    
    public int update(String sql, Connection conn, Object...params)throws SQLException{
        int effectrows = 0;
        PreparedStatement ps = null;
        List<Object> lstparams = acceptAryParams(params);
        try
        {
            if(conn == null){
                conn = DataSourceManager.getInstance().getConnection();
                isCloseConn = true;
            }
            ps = conn.prepareStatement(sql);
            if(Config.show_sql)
            {
                log.info("Execute sql:"+sql);
            }
            for(int i=1;i<=lstparams.size();i++)
            {
                ps.setObject(i,lstparams.get(i-1));
                if(Config.show_sql){
                    log.info("sql param("+i+": "+lstparams.get(i-1)+")");
                }
            }
            effectrows = ps.executeUpdate();
        }finally{
            if(ps != null){
                 ps.close();
                 if(isCloseConn&&conn!=null)conn.close();
            }
        }
        return effectrows;
    }
    
    private List<Object> acceptAryParams(Object[] params){
        List<Object> lstparams = new ArrayList<Object>();
        for(int i=0;i<params.length;i++)
        {
            if(params[i] == null){//保证sql付参个数匹配
                lstparams.add(null);
                continue;
            }
            if(params[i].getClass().isArray()){//处理数组参数
                Object[] subAry = (Object[])params[i];
                for(int j=0;j<subAry.length;j++)
                {
                    lstparams.add(subAry[j]);
                }
            }else if(params[i] instanceof List){//处理list参数
                List<?> sublst = (List<?>)params[i];
                for(int j=0;j<sublst.size();j++)
                {
                    lstparams.add(sublst.get(j));
                }
            }else{//正常传参
                lstparams.add("".equals(params[i])?"-1":params[i]);
            }
        }
        
        return lstparams;
    }
}

